﻿$(function () {
    $("#role-grid").datagrid({
        url: $.Api("Sys/Role/Grid"),
        nowrap: false, rownumbers: true, border: false,
        fit: true, singleSelect: true,
        pageSize: 20, pagination: true,
        idField: "ID", toolbar: "#role-tools",
        columns:
        [[
            { field: "Name", title: "名称", width: 200, align: "center" },
            { field: "Remark", title: "描述", width: 280, align: "center" },
            { field: "IntSort", title: "排序", width: 50, align: "center" },
            {
                field: "IsVisible", title: "启用", width: 50, align: "center",
                formatter: function (v, r, i) { return $.GetYesOrNo(v); }
            }
        ]]
    });
});

var Role = {
    //添加
    Insert: function () {
        Role.Mutual("添加", "sys/role/Edit.aspx");
    },
    //修改
    Update: function () {
        var row = $("#role-grid").datagrid("getSelected");
        if (row) {
            Role.Mutual("修改", "sys/role/Edit.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //删除
    Delete: function () {
        var row = $("#role-grid").datagrid("getSelected");
        if (row) {
            $.EasyuiConfirm("确认删除?", function (del) {
                if (del) {
                    $.AjaxMask({
                        url: $.Api("Sys/Role/Delete"),
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            result = JSON.parse(result);
                            $.EasyuiSlide(result.Message);
                            if (result.Success) { Role.Reload(); }
                        }
                    });
                }
            });
        }
        else { $.EasyuiSlide(delnullrowmsg); }
    },
    //实体操作
    Mutual: function (title, href) {
        $.ModelDialog({
            title: title, href: href, width: 260,
            handler: function () {
                $.ModelForm({
                    validate: true, button: $(this),
                    id: "#role-model",
                    url: $.Api("Sys/Role/Save"),
                    success: function (result) { Role.Reload(); }
                });
            },
            onLoad: function () { Role.FormInit(); }
        });
    },
    //弹窗初始化
    FormInit: function () {

    },

    //排序向上
    Up: function () {
        Role.Order("up");
    },
    //排序向下
    Down: function () {
        Role.Order("down");
    },
    //排序操作
    Order: function (order) {
        var row = $("#role-grid").datagrid("getSelected");
        if (row) {
            $.AjaxMask({
                url: $.Api("Sys/Role/SetOrder"), type: "post",
                data: { id: row.ID, order: order },
                success: function (result) {
                    result = JSON.parse(result);
                    $.EasyuiSlide(result.Message);
                    if (result.Success) { Role.Reload(); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //启用
    Enable: function () {
        Role.EnableOrDisable(true);
    },
    //禁用
    Disable: function () {
        Role.EnableOrDisable(false);
    },
    //启用禁用
    EnableOrDisable: function (bool) {
        var row = $("#role-grid").datagrid("getSelected");
        if (row) {
            $.AjaxMask({
                url: $.Api("Sys/Role/EnableOrDisable"),
                type: "post", data: { id: row.ID, bit: bool },
                success: function (result) {
                    result = JSON.parse(result);
                    $.EasyuiSlide(result.Message);
                    if (result.Success) { Role.Reload(); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //刷新列表
    Reload: function () {
        $.DataGridReload("#role-grid");
    },

    //分配权限
    Purview: function () {
        var row = $("#role-grid").datagrid("getSelected");
        if (row) {
            $.ModelDialog({
                title: "分配权限",
                href: "sys/role/Purview.aspx",
                toolbar: Role.PurviewTool(),
                width: $(window).width() - 20,
                height: $(window).height() - 20,
                handler: function () {
                    var button = $(this);
                    $.AjaxMask({
                        url: $.Api("Sys/Role/Purview"), type: "post",
                        data: { id: row.ID, data: Role.PurviewData() },
                        success: function (result) {
                            result = JSON.parse(result);
                            if (result.Success) {
                                $.ButtonCloseDialog(button);
                                $.EasyuiSlide(result.Message);
                            }
                            else { $.EasyuiMsg(result.Message); }
                        }
                    });
                },
                onLoad: function () { Role.PurviewInit(row.ID); }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //获取结果
    PurviewData: function () {
        var nodes = $("input:checked[edit='yes']", $("#role-purview-panel"));
        var data = [];
        for (var i = 0; i < nodes.length; i++) {
            data.push($(nodes[i]).attr("mbid"));
        }
        return data.join(",");
    },
    //分配权限工具栏
    PurviewTool: function () {
        return [{
            text: "全选", iconCls: 'icon-add', handler: function () {
                var nodes = $("input[edit='yes']", $("#role-purview-panel")).not("input:checked");
                for (var i = 0; i < nodes.length; i++) {
                    $(nodes[i]).prop("checked", true);
                }
            }
        }, {
            text: "取消", iconCls: 'icon-remove', handler: function () {
                var nodes = $("input:checked[edit='yes']", $("#role-purview-panel"));
                for (var i = 0; i < nodes.length; i++) {
                    $(nodes[i]).prop("checked", false);
                }
            }
        }, {
            text: "折叠", iconCls: 'icon-undo', handler: function () {
                $.TreeGridCollapseAll("#role-purview-grid");
            }
        }, {
            text: "展开", iconCls: 'icon-redo', handler: function () {
                $.TreeGridExpandAll("#role-purview-grid");
            }
        }];
    },
    //分配权限初始化
    PurviewInit: function (id) {
        $.AjaxMask({
            url: $.Api("Sys/Button/AllGrid"), type: "get",
            success: function (result) {
                var columns = []; result = JSON.parse(result);
                for (var i = 0; i < result.length; i++) {
                    columns.push({
                        field: result[i].Function, title: result[i].Name,
                        width: 60, align: "center",
                        formatter: function (value, row, index) {
                            return Role.PurviewFormatter(value);
                        }
                    });
                }
                Role.PurviewGrid(id, columns);
            }
        });
    },
    //获取权限表
    PurviewGrid: function (id, columns) {
        $("#role-purview-grid").treegrid({
            url: $.Api("Sys/Menu/RolePurviewGrid"),
            queryParams: { id: id }, title: "功能导航",
            nowrap: false, rownumbers: true, fit: true,
            singleSelect: true, treeField: "Name",
            idField: "ID", parentField: "Parent",
            frozenColumns: [[
                { field: "Name", title: "名称", width: 200, align: "left" }
            ]],
            columns: [columns]
        });
    },
    //获取勾选框
    PurviewFormatter: function (node) {
        if (node.disabled == "true") { return "-"; }

        if (node.value == "true") {
            return "<input edit='yes' mbid='" + node.mbid + "' type='checkbox' checked='checked' />";
        }
        else {
            return "<input edit='yes' mbid='" + node.mbid + "' type='checkbox' />";
        }
    }
};